package com.jnshu.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jnshu.dao.ManageMapper;
import com.jnshu.dto.admin.ManageDto;
import com.jnshu.dto.admin.PasswordDto;
import com.jnshu.pojo.Manage;
import com.jnshu.service.ManageService;
import com.jnshu.tools.MD5Util;
import com.jnshu.tools.binding.JsonUtil;
import com.jnshu.tools.resultUtils.PageBean;
import com.jnshu.tools.resultUtils.RUtil;
import com.jnshu.tools.resultUtils.ResultBean;
import com.jnshu.tools.resultUtils.ResultEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Martin
 * @date 2019/8/12 21:23
 */
@Slf4j
@Service
public class ManageServiceImpl implements ManageService {
    @Resource
    ManageMapper manageMapper;

    @Override
    public PageBean selectManageList(int pageNum,int pageSize) {
        Page page = PageHelper.startPage(pageNum,pageSize);
        List<Manage> roleManageList = manageMapper.selectManageList();
        log.info("ManageServiceImpl查询出来的账号列表为："+ JsonUtil.toJsonString(roleManageList));
        if (roleManageList == null){
            log.info("ManageServiceImpl查询数据为空");
            return RUtil.okDefault(0,null,pageNum,pageSize);
        }
        return RUtil.okDefault(page.getTotal(),roleManageList,pageNum,pageSize);
    }

    @Override
    public ResultBean deleteByPrimaryKey(Long id) {
        log.info("ManageServiceImpl删除的账户id为："+id);
        int result = manageMapper.deleteByPrimaryKey(id);
        log.info("ManageServiceImpl的删除结果为："+result);
        if (result == 1){
            return RUtil.ok();
        }else {
            return RUtil.error(ResultEnum.NOTHINGNESS);
        }
    }

    @Override
    public ResultBean insertSelective(ManageDto manageDto,String loginId) {
        Manage manage = manageMapper.selectByManageName(manageDto.getManageName());
        if (manage != null){
            log.info("ManageServiceImpl查出来的manage不为空，已有manage："+manage);
            return RUtil.error(ResultEnum.SAME_NAME);
        }
        String password = MD5Util.getSaltMD5(manageDto.getPassword());
        log.info("ManageServiceImpl中加密后的manage密码为："+password);
        Manage newManage = new Manage();
        newManage.setName(manageDto.getManageName());
        newManage.setPassword(password);
        newManage.setRoleId(manageDto.getRoleId());
        newManage.setCreateAt(System.currentTimeMillis());
        newManage.setUpdateAt(System.currentTimeMillis());
        newManage.setCreateBy(Long.valueOf(loginId));
        newManage.setUpdateBy(Long.valueOf(loginId));
        if (manageMapper.insertSelective(newManage) == 1){
            return RUtil.ok();
        }
        return RUtil.error(ResultEnum.ERROR_ADD);
    }

    @Override
    public ResultBean updateByPrimaryKeySelective(ManageDto manageDto,String loginId) {
        log.info("ManageServiceImpl将要修改的Manage信息先调出来");
        Manage manage = manageMapper.selectByPrimaryKey(manageDto.getId());
        log.info("ManageServiceImpl根据传入数据id查询出要修改的manage："+manage);
        if (manage != null){
            if (manageDto.getManageName().equals("")){
                return RUtil.error(ResultEnum.SAME_NAME);
            }
            manage.setName(manageDto.getManageName());
            if(!manageDto.getPassword().equals("")) {
                manage.setPassword(MD5Util.getSaltMD5(manageDto.getPassword()));
            }
            manage.setRoleId(manageDto.getRoleId());
            manage.setUpdateAt(System.currentTimeMillis());
            manage.setUpdateBy(Long.valueOf(loginId));
            if (manageMapper.updateByPrimaryKeySelective(manage) == 1){
                return RUtil.ok();
            }
        }
        return RUtil.error(ResultEnum.ERROR_NULL_MANAGE);
    }

    @Override
    public PageBean selectByRoleId(Long id,int pageNum,int pageSize) {
        Page page = PageHelper.startPage(pageNum,pageSize);
        List<Manage> roleManageList = manageMapper.selectByRoleId(id);
        log.info("ManageServiceImpl根据权限查询出来的账号列表为："+ JsonUtil.toJsonString(roleManageList));
        if (roleManageList == null){
            log.info("ManageServiceImpl根据权限查询数据为空");
            return RUtil.okDefault(0,null,pageNum,pageSize);
        }
        return RUtil.okDefault(page.getTotal(),roleManageList,pageNum,pageSize);
    }

    @Override
    public ResultBean updateByLoginPassword(PasswordDto passwordDto,String loginId) {
        Long id = Long.valueOf(loginId);
        log.info("获取的登录用户id为："+id);
        Manage manage = manageMapper.selectByPrimaryKey(id);
        if (manage ==null){
            log.info("ManageServiceImpl根据登录id查询的账号为空");
            return RUtil.error(ResultEnum.FAILURE);
        }
        log.info("ManageServiceImpl根据登录id查询出来的账号为："+manage);
        boolean result = MD5Util.getSaltVerifyMD5(passwordDto.getOldPassword(),manage.getPassword());
        log.info("ManageServiceImpl验证原密码是否正确："+result);
        if (!result){
            return RUtil.error(ResultEnum.ERROR_PASSWORD);
        }
        log.info("更新用户登录密码");
        manage.setPassword(MD5Util.getSaltMD5(passwordDto.getNewPassword()));
        manage.setUpdateAt(System.currentTimeMillis());
        manage.setUpdateBy(id);
        if (manageMapper.updateByPrimaryKeySelective(manage)==1){
            return RUtil.ok();
        }
        return RUtil.error(ResultEnum.FAILURE);
    }

    @Override
    public ResultBean selectByManageId(Long id) {
        log.info("ManageServiceImpl查询的id为："+id);
        Manage manage = manageMapper.selectByPrimaryKey(id);
        log.info("ManageServiceImpl根据登录id查询出来的账号为："+manage);
        if (manage == null){
            log.info("ManageServiceImpl根据id查询出来的账号为空");
            RUtil.error(ResultEnum.NOTHINGNESS);
        }
        return RUtil.ok(manage);
    }
}
